package com.ruiyun.ruinews.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.ruiyun.ruinews.R;
import com.ruiyun.ruinews.entity.Recyclephoto;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zouhui on 16/1/8.
 */
public class RecyclePhotoAdpater extends PagerAdapter{
    private List<View> viewData;
    private List<Recyclephoto> photoData;
    private Context context;
    private ImageLoader imageLoader;

    public RecyclePhotoAdpater(List<Recyclephoto> photoData, Context context) {
        this.photoData = photoData;
        this.context = context;
        init();
    }

    private void init() {
        viewData = new ArrayList<>();
        for (int i = 0; i < photoData.size(); i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_newscalss_turns,null);
            viewData.add(view);
        }
    }

    @Override
    public int getCount() {
        if (photoData.size()>1)
        return Integer.MAX_VALUE;
        else{
            return photoData.size();
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int index = position%photoData.size();
        View view = viewData.get(position);
        // TODO: 16/1/8 找imageVIew
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_newsclass_turns_one);
        imageLoader.displayImage(photoData.get(index).getUrl(),imageView);
        container.addView(view);
        return container;
    }
}
